package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsArticleMapper;
import com.qf.pojo.DtsArticle;
import com.qf.service.DtsArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

@Service
public class DtsArticleServiceImpl implements DtsArticleService {
    @Autowired
    DtsArticleMapper articleMapper;
    @Override
    public IPage<DtsArticle> findByPage(Integer page, Integer limit, String sort, String order, String title) {
        IPage<DtsArticle> iPage = new Page<>(page,limit);
        QueryWrapper<DtsArticle> qw = new QueryWrapper<>();
        if(!StringUtils.isEmpty(title)){
            qw.like("title",title);
        }
        if(!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            if(order.equalsIgnoreCase("desc")){
                qw.orderByDesc(sort);
            }else {
                qw.orderByAsc(sort);
            }
        }



        articleMapper.selectPage(iPage,qw);
        return iPage;
    }

    @Override
    public DtsArticle add(DtsArticle dtsArticle) {
        dtsArticle.setAddTime(new Date());
        dtsArticle.setDeleted(false);
        articleMapper.insert(dtsArticle);
        return dtsArticle;
    }

    @Override
    public void update(DtsArticle dtsArticle) {
        dtsArticle.setUpdateTime(new Date());
        dtsArticle.setDeleted(false);
        articleMapper.updateById(dtsArticle);
    }

    @Override
    public DtsArticle findById(Integer id) {
        DtsArticle article = articleMapper.selectById(id);
        return article;
    }

    @Override
    public void delete(DtsArticle dtsArticle) {
        articleMapper.deleteById(dtsArticle);
    }
}
